import { MockMethod } from 'vite-plugin-mock'

// 模拟数据
function myMockDataFun() {
  return [
    {
      id: 1,
      username: 'admin',
      password: '111111',
      // 暂且固定
      token:
        'eyJhbGciOiJIUzUxMiIsInppcCI6IkdaSVAifQ.H4sIAAAAAAAAAKtWKi5NUrJSSkzJzcxT0lFKrShQsjI0MzQzMTK1NDGvBQApMebnIAAAAA.UG2Qj7F7ASMLBugNG67-wKs0Un6ORrneMkSdio0rKhX0o-fd6BC24T1gCzfVVtsNERPzs4Sjwz8JOyPfLazUSA',
      role: 'admin',
      perms: [1, 2, 3],
      avatar: 'http://electronjs.p2hp.com/assets/img/logo.svg'
    },
    {
      id: 2,
      username: 'wright',
      password: '111111',
      token:
        'eyJhbGciOiJIUzUxMiIsInppcCI6IkdaSVAifQ.H4sIAAAAAAAAAKtWKi5NUrJSSkzJzcxT0lFKrShQsjI0MzQzMTK1NDGvBQApMebnIAAAAA.UG2Qj7F7ASMLBugNG67-wKs0Un6ORrneMkSdio0rKhX0o-fd6BC24T1gCzfVVtsNERPzs4Sjwz8JOyPfLazUSA',
      role: 'dev',
      perms: [1, 2],
      avatar: 'http://electronjs.p2hp.com/assets/img/logo.svg'
    }
  ]
}

export default [
  {
    url: '/dev-api/v1/login',
    method: 'post',
    response: ({ body }) => {
      console.log('query', body)
      const { username, password } = body
      const myMockData = myMockDataFun()
      const curUser = myMockData.find(
        (item) => item.username === username && item.password === password
      )
      if (!curUser) {
        return { code: 201, data: null, msg: '账号或密码不正确' }
      }
      return {
        code: 200,
        data: {
          token: curUser.token
        },
        msg: '登录成功'
      }
    }
  },
  {
    url: '/dev-api/v1/getUserInfo',
    method: 'get',
    response: ({ query }) => {
      const { token } = query
      const myMockData = myMockDataFun()
      const curUser = myMockData.find((item) => item.token === token)
      if (!curUser) {
        return { code: 201, data: null, msg: '该用户token不存在' }
      }
      return {
        code: 200,
        data: {
          ...curUser
        },
        msg: 'succss'
      }
    }
  }
  //   {
  //     url: '/api/text',
  //     method: 'post',
  //     rawResponse: async (req, res) => {
  //       let reqbody = ''
  //       await new Promise((resolve) => {
  //         req.on('data', (chunk) => {
  //           reqbody += chunk
  //         })
  //         req.on('end', () => resolve(undefined))
  //       })
  //       res.setHeader('Content-Type', 'text/plain')
  //       res.statusCode = 200
  //       res.end(`hello, ${reqbody}`)
  //     }
  //   }
] as MockMethod[]
